<?php
require_once( dirname(__FILE__) . '/Unserializer.php' );
require_once 'XML/Serializer.php';
require_once dirname(__FILE__) . '/Abstract/Xml.php';

class Lib_Xml extends Lib_Abstract_Xml
{
	/**
	 * アンシリアライズオプション配列
	 * @var array
	 */
	protected $_aryUnsOption;

	/**
	 * 選択オプション
	 * @var array
	 */
	protected $_arySelOption = '';

	/**
	 * XML保持
	 * @var array
	 */
	public    $aryXml;

	/**
	 * XMLファイル名保持
	 * @var string
	 */
	protected $_strFileName;

	/**
	 * コンストラクタ
	 * @return void
	 */
	public function __construct()
	{
		if ( $this->_arySelOption == '' ) {
			$this->_arySelOption = array(
			"indent" => "\t",
			"linebreak" => "\n",
			"typeHints" => false,
			"addDecl" => true,
			"encoding" => "UTF-8",
			"rootName" => "DATA ",
    		"mode" => "simplexml"
			);
		}

		$this->_strFileName = '';
	}

	/**
	 * XMLファイルを読み込む
	 * @param string $strFileName
	 * @return &array
	 */
	public function &loadFromFile( $strFileName, $aryUnsOption=array() )
	{
		$bResult = FALSE;
		if ( !file_exists( $strFileName) ) {
			return $bResult;;
		}

		if ( count( $aryUnsOption ) > 0 ) {
			$this->_setForceEnum( $aryUnsOption );
		}

		$aryXml = file_get_contents( $strFileName );
		$this->_strFileName = $strFileName;

		$objUnserializer = new Lib_Unserializer( $this->_aryUnsOption );
		if ( !$objUnserializer->unserialize( $aryXml ) ) {
			return $bResult;
		}
		$this->aryXml = $objUnserializer->getUnserializedData();
		return $this->aryXml;
	}

	/**
	 * 配列をシリアライズしてテキストファイルに保存する
	 * @see Lib/Abstract/Lib_Abstract_Xml#saveToFile($strFileName)
	 */
	public function saveToFile( $strFileName = '')
	{
		if ( $strFileName == '' ) {
			if ( $this->_strFileName == '' ) {
				return FALSE;
			}
			$strFileName = $this->_strFileName;
		}

		$aryEntitiedXml = $this->htmlentity();
//		$this->_html_entity_decode($aryEntitiedXml);

		$objSerializer = new XML_Serializer($this->_arySelOption);
		if ( !$objSerializer->serialize( $aryEntitiedXml ) ) {
			return FALSE;
		}
		$aryXml = $objSerializer->getSerializedData();
		file_put_contents( $strFileName, $aryXml );
	}

	public function htmlentity()
	{
		$aryResultXml = $this->aryXml;
//		$this->_htmlentityRecur($aryResultXml);
		return $aryResultXml;
	}

	protected function _htmlentityRecur( &$aryDest ) {
		foreach ( $aryDest as &$dest ) {
			if ( is_array($dest) ) {
				$this->_htmlentityRecur($dest);
			} else {
				$dest = htmlspecialchars($dest);
			}
		}
	}

	public function html_entity_decode()
	{
		$aryResultXml = $this->aryXml;
		$this->_html_entity_decode( $aryResultXml );
		return $aryResultXml;
	}

	protected function _html_entity_decode( &$aryDest )
	{
		foreach ( $aryDest as &$dest ) {
			if ( is_array( $dest ) ) {
				$this->_html_entity_decode( $dest );
			} else {
				$dest = htmlspecialchars_decode( $dest );
			}
		}
	}

	/**
	 * 仮想メソッドダミーメソッド
	 * @see Lib/Abstract/Lib_Abstract_Xml#_setForceEnum()
	 */
	public function _setForceEnum( $aryForceEnum = array() )
	{
		$this->_aryForceEnum = array();
		$this->_aryUnsOption = array();
		if ( count ( $aryForceEnum ) > 0 ) {
			$this->_aryForceEnum = array(
				"forceEnum"=>$aryForceEnum
			);
			$this->_aryUnsOption = $this->_aryForceEnum;
		}
	}

}